Imports System.Resources

Namespace Helpers

    Public Class UITranslationHelper

        Public Sub New(ByVal resourceManager As ResourceManager)
            Me.ResourceManager = resourceManager
        End Sub

        Private _resourceManager As ResourceManager
        Property ResourceManager() As ResourceManager
            Get
                Return _resourceManager
            End Get
            Set(ByVal value As ResourceManager)
                _resourceManager = value
            End Set
        End Property

        Public Sub Translate(ByVal controls As Control.ControlCollection)
            For Each c As Control In controls
                If c.GetType() Is GetType(Label) Then
                    TranslateLabel(CType(c, Label))
                End If
                If c.GetType() Is GetType(Button) Then
                    TranslateButton(CType(c, Button))
                End If
                If c.GetType() Is GetType(MenuStrip) Then
                    TranslateMenuStrip(CType(c, MenuStrip).Items)
                End If
                Translate(c.Controls)
            Next
        End Sub

        Public Sub TranslateGridColumn(ByVal column As DataGridViewColumn)

            column.HeaderText = Me.ResourceManager.GetString(column.Name + ".HeaderText")

        End Sub

        Private Sub TranslateMenuStrip(ByVal items As ToolStripItemCollection)
            For Each item As ToolStripMenuItem In items
                item.Text = Me.ResourceManager.GetString(item.Name + ".Text")
                If item.HasDropDownItems Then
                    TranslateMenuStrip(item.DropDownItems)
                End If
            Next
        End Sub

        Private Sub TranslateButton(ByVal b As Button)
            b.Text = Me.ResourceManager.GetString(b.Name + ".Text")
        End Sub

        Private Sub TranslateLabel(ByVal l As Label)
            l.Text = Me.ResourceManager.GetString(l.Name + ".Text")
        End Sub

    End Class

End Namespace

